package com.hgzy.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgzy.sys.common.Constant;
import com.hgzy.sys.common.DataGridView;
import com.hgzy.sys.domain.Loginfo;
import com.hgzy.sys.service.LoginfoService;
import com.hgzy.sys.vo.LogInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * 登陆控制器
 * @author : look-word
 * @date : 2022-01-14 20:18
 **/

@Slf4j
@RestController
@RequestMapping("/loginfo")
public class LoginfoController  extends BaseController{

    @Autowired
    private LoginfoService loginfoService;
    /**
     * 查询全部的日志信息
     * @return 包含全部登陆日志的对象
     */
    @RequestMapping("/loadAllLoginfo")
    public DataGridView loadAllLogInfo(LogInfoVo loginfoVo, HttpSession session) {
        IPage<Loginfo> page=new Page<>(loginfoVo.getPage(), loginfoVo.getLimit());
        QueryWrapper<Loginfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(loginfoVo.getLoginname()),"loginname", loginfoVo.getLoginname());
        queryWrapper.like(StringUtils.isNotBlank(loginfoVo.getLoginip()), "loginip",loginfoVo.getLoginip());
        queryWrapper.ge(loginfoVo.getStartTime()!=null, "logintime", loginfoVo.getStartTime());
        queryWrapper.le(loginfoVo.getEndTime()!=null, "logintime", loginfoVo.getEndTime());
        queryWrapper.orderByDesc("logintime");
        this.loginfoService.page(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     *根据指定id 删除 登陆日志
     */
    @RequestMapping("deleteLoginfo")
    public DataGridView deleteLoginfo(Integer id){
        log.info(id+"");
        boolean removeById = loginfoService.removeById(id);
        return new DataGridView(Constant.OK,removeById?Constant.DELETE_OK_MSG:Constant.DELETE_ERROR_MSG);
    }
    /**
     * 根据id批量删除
     */
    @RequestMapping("batchDeleteLoginfo")
    public DataGridView batchDeleteById(LogInfoVo logInfoVo){
        Integer[] ids = logInfoVo.getIds();
        log.info(ids+"");
        boolean removeById = loginfoService.removeByIds(Arrays.asList(ids));
        return new DataGridView(Constant.OK,removeById?Constant.DELETE_OK_MSG:Constant.DELETE_ERROR_MSG);
    }
}
